package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.order.service.DynamicService;
import com.wing.product.dao.ProductPackDao;
import com.wing.product.model.entity.ProductPack;
import com.wing.product.model.request.ProductPackForm;
import com.wing.product.model.response.ProductPackVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 产品包装-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProductPackServiceImpl extends ServiceImpl<ProductPackDao, ProductPack> implements ProductPackService {

    @Autowired
    private DynamicService dynamicService;

    @Override
    public int add(ProductPackForm productPackForm) {

        //校验

        ProductPack productPack =new ProductPack();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProductPack.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productPackForm, productPack, copyOptions);
        String code=getCode();
        productPack.setNo(Integer.valueOf(code));
        productPack.setCode(productPackForm.getProductTypeCode()+"-"+productPackForm.getProductUnitCode()+"-"+code);
        return baseMapper.insert(productPack);
    }

    private String getCode(){
        return dynamicService.getDayNextCode("pro_product_pack");
    }

    @Override
    public List<ProductPackVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductPackVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProductPackVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductPackVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public ProductPackVO getPackUnitById(Long id) {
        return baseMapper.getPackUnitById(id);
    }

    @Override
    public ProductPackVO getDefaultPack() {
        return baseMapper.getDefaultPack();
    }
}
